package com.trace.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.ShipAddress;
import com.trace.app.service.ShipAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 收货地址(ShipAddress)表控制层
 *
 * @author lionkliu
 * @since 2024-04-02 19:08:53
 */
@RestController
@RequestMapping("shipAddress")
public class ShipAddressController {

    @Resource
    private ShipAddressService shipAddressService;

    @GetMapping("/page")
    public ResponseResult selectPage(@RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize) {
        return shipAddressService.selectPage(pageNum, pageSize);
    }

    @GetMapping("/user/{userId}")
    public ResponseResult getByUserId(@PathVariable Integer userId) {
        return ResponseResult.okResult(shipAddressService.list(new LambdaQueryWrapper<ShipAddress>().eq(ShipAddress::getUserId, userId).orderByDesc(ShipAddress::getRank)));
    }

    @GetMapping("{id}")
    public ResponseResult selectOne(@PathVariable Integer id) {
        return shipAddressService.selectOne(id);
    }

    @PostMapping
    public ResponseResult insert(@RequestBody ShipAddress shipAddress) {
        return shipAddressService.insertOne(shipAddress);
    }

    @PutMapping
    public ResponseResult update(@RequestBody ShipAddress shipAddress) {
        return shipAddressService.updateOne(shipAddress);
    }

    @PutMapping("/def")
    public ResponseResult setDefault(@RequestBody ShipAddress shipAddress) {
        return shipAddressService.setDefault(shipAddress);
    }


    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Integer id) {
        return shipAddressService.deleteById(id);
    }

}
